<template>
  <div class="login">

    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">战天下后台管理系统</h3>
      <el-form-item prop="mobile">
        <el-input v-model="loginForm.mobile" type="text" auto-complete="off" placeholder="请输入手机号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item v-if="captchaEnabled" prop="code">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <el-button type="primary" :disabled="canClick" @click="getCode">{{ content }}</el-button>
        </div>
      </el-form-item>
      <el-checkbox
        v-model="loginForm.rememberMe"
        class="checkrember"
        style="margin:0px 0px 25px 0px;"
      >记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div v-if="register" style="float: right;">
          <router-link class="link-type" :to="'/register'">申请入驻</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2022 zhantianxia All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import { getcodeAPI } from '../api/login'
export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      loginForm: {
        mobile: '13581472516',
        code: ''
      },
      loginRules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: ['blur'] }
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
      content: '获取验证码', // 按钮初始的文字
      time: 60, // 倒计时时间
      canClick: false, // 可点击获取验证码条件
      toSubmit: 0 // 是否提交过

    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCookie()
  },
  methods: {
    // 点击事件拿到验证码
    // js
    async getCode() {
      // 判断是否可点击
      if (this.canClick) return
      await getcodeAPI(this.loginForm.mobile).then(res => {
        console.log(res.message)
        if (res.errcode === 1) {
          this.$message({
            message: '验证码发送成功',
            type: 'success'
          })
          console.log('成功')
          this.canClick = true
          this.content = this.time + 's后可发送'
          // 时间自减
          const clock = window.setInterval(() => {
            this.time--
            this.content1 = this.time + 'sTente'
            // 时间小于0的时候恢复
            if (this.time < 0) {
              window.clearInterval(clock)
              this.content = '提交'
              this.time = 60
              this.canClick = true
            }
          }, 1000)
        }
      })
        .catch(err => {
          console.log(err)
        })
    },

    getCookie() {
      const mobile = Cookies.get('mobile')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        mobile: mobile === undefined ? this.loginForm.mobile : mobile,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('mobile', this.loginForm.mobile, { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove('mobile')
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' }).catch(() => { })
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background: url(../assets/images/login-background.jpeg);
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
